package com.example.bishe.util;


import org.springframework.beans.factory.annotation.Value;

/**
 * 加密
 *
 * @author: LDH
 * @date: 2023年04月08日 20:42
 */
public class Encrypt {

    private final static String key = "jkdgxvhasjbd345";

    public static String jiami(String str) {
        String keyBinary = BinaryStringConverteUtil.toBinaryString(key);
        String binary = BinaryStringConverteUtil.toBinaryString(str);

        StringBuffer sb = new StringBuffer(binary);
        sb.insert(0, keyBinary);
        sb.insert(9+18, keyBinary);
        sb.insert(18+18 * 2, keyBinary);

        String s = removeElements(sb.toString());

        String s1 = reverseString(s);

        String s2 = BinaryStringConverteUtil.splitToBinary(s1);

        String s3 = BinaryStringConverteUtil.toString(s2);

        return s3;
    }


    // 去除元素
    public static String removeElements(String str) {
        if (str == null || str.isEmpty()) {
            return str;
        }

        int len = str.length();
        if (len <= 1) {
            return str;
        }

        StringBuffer result = new StringBuffer(str);
        int max = 15 * 6;

        for (int i = 0; i < len; i++){
            if (len == max) return result.toString();
            result.deleteCharAt(i);
            len = result.length();
        }

        return removeElements(result.toString());

    }

    // 二进制字符串逆序
    public static String reverseString(String str) {
        return new StringBuilder(str).reverse().toString();
    }


}